package com.ggb.securityauto.controller;

import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: GGB
 * @Date: 2021/11/23
 * @Description
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 获取当前用户
     * @param authentication
     * @return
     */
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request){
//        解析jwt中的内容
        String head=request.getHeader("Authorization");
        String token=head.substring(head.indexOf("bearer")+7);
        return Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
//        return authentication.getPrincipal();
    }
}
